<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Mocha笔记 | latte and cat</title>
    <meta name="description" content="study programs and record life">
    <meta name="generator" content="VuePress 1.4.0">
    <link rel="icon" href="/favicon.ico">
  <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/assets/css/0.styles.e1c67194.css" as="style"><link rel="preload" href="/assets/js/app.ff77de4f.js" as="script"><link rel="preload" href="/assets/js/4.6c1b7be2.js" as="script"><link rel="preload" href="/assets/js/1.3ea07518.js" as="script"><link rel="preload" href="/assets/js/16.b24685ca.js" as="script"><link rel="prefetch" href="/assets/js/10.18024e3f.js"><link rel="prefetch" href="/assets/js/11.a9ba7471.js"><link rel="prefetch" href="/assets/js/12.64cdb496.js"><link rel="prefetch" href="/assets/js/13.9fb4b156.js"><link rel="prefetch" href="/assets/js/14.c3753656.js"><link rel="prefetch" href="/assets/js/15.652b11fc.js"><link rel="prefetch" href="/assets/js/17.67155a52.js"><link rel="prefetch" href="/assets/js/18.1d703305.js"><link rel="prefetch" href="/assets/js/19.0b6a6748.js"><link rel="prefetch" href="/assets/js/20.a576349c.js"><link rel="prefetch" href="/assets/js/21.4bd94152.js"><link rel="prefetch" href="/assets/js/22.1abe4122.js"><link rel="prefetch" href="/assets/js/23.d2352828.js"><link rel="prefetch" href="/assets/js/24.a69cf77c.js"><link rel="prefetch" href="/assets/js/25.40b2c71a.js"><link rel="prefetch" href="/assets/js/26.7da4b904.js"><link rel="prefetch" href="/assets/js/27.80a148eb.js"><link rel="prefetch" href="/assets/js/28.d8cf3072.js"><link rel="prefetch" href="/assets/js/5.60884555.js"><link rel="prefetch" href="/assets/js/6.e3bfbe92.js"><link rel="prefetch" href="/assets/js/7.f4dfd5c4.js"><link rel="prefetch" href="/assets/js/8.14ec5fab.js"><link rel="prefetch" href="/assets/js/9.e4d60889.js"><link rel="prefetch" href="/assets/js/vendors~flowchart.af2c2885.js">
    <link rel="stylesheet" href="/assets/css/0.styles.e1c67194.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div><div class="theme-container" data-v-319dd33c><div data-v-319dd33c><div id="loader-wrapper" class="loading-wrapper" data-v-4b73742e data-v-319dd33c data-v-319dd33c><div class="loader-main" data-v-4b73742e><div data-v-4b73742e></div><div data-v-4b73742e></div><div data-v-4b73742e></div><div data-v-4b73742e></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-1e2a0cc0 data-v-319dd33c data-v-319dd33c><h3 class="title" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0>latte and cat</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0><input type="password" value="" data-v-1e2a0cc0> <span data-v-1e2a0cc0>Konck! Knock!</span> <button data-v-1e2a0cc0>OK</button></label> <div class="footer" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0><span data-v-1e2a0cc0><i class="iconfont reco-theme" data-v-1e2a0cc0></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-1e2a0cc0>vuePress-theme-reco</a></span> <span data-v-1e2a0cc0><i class="iconfont reco-copyright" data-v-1e2a0cc0></i> <a data-v-1e2a0cc0><span data-v-1e2a0cc0>橘子</span>
            
          <span data-v-1e2a0cc0>2019 - </span>
          2020
        </a></span></div></div> <div class="hide" data-v-319dd33c><header class="navbar" data-v-319dd33c><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/head.png" alt="latte and cat" class="logo"> <span class="site-name">latte and cat</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  Home
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      Category
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/笔记/" class="nav-link"><i class="iconfont undefined"></i>
  笔记
</a></li><li class="dropdown-item"><!----> <a href="/categories/前端/" class="nav-link"><i class="iconfont undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/categories/摘记/" class="nav-link"><i class="iconfont undefined"></i>
  摘记
</a></li><li class="dropdown-item"><!----> <a href="/categories/随笔/" class="nav-link"><i class="iconfont undefined"></i>
  随笔
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><a href="/note/" class="nav-link router-link-active"><i class="iconfont reco-document"></i>
  Notes
</a></div><div class="nav-item"><a href="/timeLine/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><a href="http://photo.smallsunnyfox.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-blog"></i>
  PhotoAlbum
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      Contact
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/smallsunnyfox" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-319dd33c></div> <aside class="sidebar" data-v-319dd33c><div class="personal-info-wrapper" data-v-6c8ffc9c><!----> <h3 class="name" data-v-6c8ffc9c>
    橘子
  </h3> <div class="num" data-v-6c8ffc9c><div data-v-6c8ffc9c><h3 data-v-6c8ffc9c>14</h3> <h6 data-v-6c8ffc9c>文章</h6></div> <div data-v-6c8ffc9c><h3 data-v-6c8ffc9c>6</h3> <h6 data-v-6c8ffc9c>标签</h6></div></div> <hr data-v-6c8ffc9c></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  Home
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      Category
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/笔记/" class="nav-link"><i class="iconfont undefined"></i>
  笔记
</a></li><li class="dropdown-item"><!----> <a href="/categories/前端/" class="nav-link"><i class="iconfont undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/categories/摘记/" class="nav-link"><i class="iconfont undefined"></i>
  摘记
</a></li><li class="dropdown-item"><!----> <a href="/categories/随笔/" class="nav-link"><i class="iconfont undefined"></i>
  随笔
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><a href="/note/" class="nav-link router-link-active"><i class="iconfont reco-document"></i>
  Notes
</a></div><div class="nav-item"><a href="/timeLine/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><a href="http://photo.smallsunnyfox.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-blog"></i>
  PhotoAlbum
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      Contact
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/smallsunnyfox" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>HTML5</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/note/html5/HTML5的语义元素.html" class="sidebar-link">HTML5语义元素</a></li><li><a href="/note/html5/HTML5多媒体标签.html" class="sidebar-link">HTML5多媒体标签</a></li><li><a href="/note/html5/HTML5表单元素.html" class="sidebar-link">HTML5表单元素</a></li><li><a href="/note/html5/HTML5中的API.html" class="sidebar-link">HTML5中的API</a></li><li><a href="/note/html5/Canvas.html" class="sidebar-link">Canvas</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>CSS</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/note/css/css3新特性.html" class="sidebar-link">CSS3新特性</a></li><li><a href="/note/css/css3Flip.html" class="sidebar-link">CSS3实现翻转(Flip)效果</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>前端单元测试</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/note/fe-unit-test/chai.html" class="sidebar-link">chai.js笔记</a></li><li><a href="/note/fe-unit-test/mocha.html" class="active sidebar-link">Mocha笔记</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/note/fe-unit-test/mocha.html#断言" class="sidebar-link">断言</a></li><li class="sidebar-sub-header"><a href="/note/fe-unit-test/mocha.html#异步测试" class="sidebar-link">异步测试</a></li><li class="sidebar-sub-header"><a href="/note/fe-unit-test/mocha.html#同步测试" class="sidebar-link">同步测试</a></li><li class="sidebar-sub-header"><a href="/note/fe-unit-test/mocha.html#箭头函数" class="sidebar-link">箭头函数</a></li><li class="sidebar-sub-header"><a href="/note/fe-unit-test/mocha.html#钩子" class="sidebar-link">钩子</a></li><li class="sidebar-sub-header"><a href="/note/fe-unit-test/mocha.html#测试配置" class="sidebar-link">测试配置</a></li><li class="sidebar-sub-header"><a href="/note/fe-unit-test/mocha.html#动态生成" class="sidebar-link">动态生成</a></li><li class="sidebar-sub-header"><a href="/note/fe-unit-test/mocha.html#测试耗时" class="sidebar-link">测试耗时</a></li><li class="sidebar-sub-header"><a href="/note/fe-unit-test/mocha.html#测试超时" class="sidebar-link">测试超时</a></li><li class="sidebar-sub-header"><a href="/note/fe-unit-test/mocha.html#安装使用" class="sidebar-link">安装使用</a></li><li class="sidebar-sub-header"><a href="/note/fe-unit-test/mocha.html#测试报告" class="sidebar-link">测试报告</a></li></ul></li><li><a href="/note/fe-unit-test/vueTestUtils.html" class="sidebar-link">VueTestUtils笔记</a></li></ul></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-1e2a0cc0 data-v-319dd33c><h3 class="title" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0>Mocha笔记</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0><input type="password" value="" data-v-1e2a0cc0> <span data-v-1e2a0cc0>Konck! Knock!</span> <button data-v-1e2a0cc0>OK</button></label> <div class="footer" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0><span data-v-1e2a0cc0><i class="iconfont reco-theme" data-v-1e2a0cc0></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-1e2a0cc0>vuePress-theme-reco</a></span> <span data-v-1e2a0cc0><i class="iconfont reco-copyright" data-v-1e2a0cc0></i> <a data-v-1e2a0cc0><span data-v-1e2a0cc0>橘子</span>
            
          <span data-v-1e2a0cc0>2019 - </span>
          2020
        </a></span></div></div> <div data-v-319dd33c><main class="page"><div class="page-title" style="display:none;"><h1>Mocha笔记</h1> <hr> <div data-v-484a899e><i class="iconfont reco-account" data-v-484a899e><span data-v-484a899e>橘子</span></i> <!----> <i class="iconfont reco-eye" data-v-484a899e><span id="/note/fe-unit-test/mocha.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-484a899e><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="iconfont reco-tag tags" data-v-484a899e><span class="tag-item" data-v-484a899e>
      单元测试
    </span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><h2 id="断言"><a href="#断言" class="header-anchor">#</a> 断言</h2> <p>Mocha允许你使用任意你喜欢的断言库，在上面的例子中，我们使用了Node.js内置的assert模块作为断言。如果能够抛出一个错误，它就能够运行。这意味着你能使用下面的这些仓库，比如：</p> <ul><li>should.js</li> <li>expect.js</li> <li>chai</li> <li>better-assert</li> <li>unexpected</li></ul> <h2 id="异步测试"><a href="#异步测试" class="header-anchor">#</a> 异步测试</h2> <ul><li>回调函数：通常命名为done()给it()方法，done()接受一个err</li> <li>promise：
<ul><li>除了使用done()回调函数，你也可以返回一个Promise,这可以测试那些返回promise的方法</li> <li>返回一个promise的同时调用done函数将会导致一个异常。</li></ul></li> <li>async/await</li></ul> <h2 id="同步测试"><a href="#同步测试" class="header-anchor">#</a> 同步测试</h2> <p>当测试同步代码的时候，可以省略参数中的回调函数，Mocha会自动的测试代码。</p> <h2 id="箭头函数"><a href="#箭头函数" class="header-anchor">#</a> 箭头函数</h2> <p>向Mocha传递箭头函数是不好的，由于this的词法作用域的问题，箭头函数是不能够访问mocha的上下文的。</p> <h2 id="钩子"><a href="#钩子" class="header-anchor">#</a> 钩子</h2> <blockquote><p>测试可以出现在before,after或者和你的钩子函数交替出现。钩子函数会按照它们被定义的顺序运行。一般就是，<code>before()(只运行一次) =&gt; beforeEach() =&gt; afterEach() =&gt; after()(只运行一次)</code>。</p></blockquote> <ul><li>before：在这个区块内的所有测试之前运行</li> <li>after：在这个区块内的所有测试之后运行</li> <li>beforeEach：在这个区块内的每个测试运行之前运行</li> <li>afterEach：在这个区块内的每个测试之后运行</li></ul> <blockquote><p>所有的钩子可以是同步的也可以是异步的，其行为就像是普通的测试用例。例如，你希望在每个测试之前，向数据库中填充一些内容。</p></blockquote> <p>你可以选择几个文件来添加根级别的钩子。例如，添加<code>beforeEach()</code>在所有<code>describe()</code>块外面(译者注：可以理解为最顶级作用域中)，这会造成在每个测试用例之前调用这个钩子函数。不仅仅它所在的这个文件(这是因为Mocha有一个暗藏的<code>describe()</code>，叫做&quot;root-suite&quot;)。</p> <h2 id="测试配置"><a href="#测试配置" class="header-anchor">#</a> 测试配置</h2> <ul><li>delay：使用<code>mocha --delay</code> 在mocha命令运行之后，先做一些别的工作，再启动测试。</li> <li>pending：不给测试用例传递一个回调函数，就是被等待实现的测试用例，但同样会在报告中体现出来。</li> <li>.only()：在用例测试集或者用例单元后面加上此方法，可以让mocha只测试此用例集合或者用例单元。</li> <li>.skip()：和<code>only()</code>方法相反，<code>.skip()</code>方法可以用于跳过某些测试测试集合和测试用例。所有被跳过的用例都会被标记为<code>pending</code>用例，在报告中也会以<code>pending</code>用例显示。</li> <li>retry：Mocha允许你为失败的测试用例指定需要重复的次数。这个功能是为端对端测试所设计的，因为这些测试的数据不好模拟。Mocha不推荐在单元测试中使用这个功能。这个功能会重新运行beforeEach和afterEach钩子，但不会重新运行before/after钩子。
使用<code>this.retries(4)</code> 设置</li></ul> <h2 id="动态生成"><a href="#动态生成" class="header-anchor">#</a> 动态生成</h2> <p>Mocha可以使用<code>Function.prototype.call</code>和函数表达式来定义测试用例，其实就是动态生成一些测试用例，不需要使用什么特殊的语法。和你见过的其他框架可能有所不同，这个特性可以通过定义一些参数来实现测试用例所拥有的功能。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> assert <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'chai'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>assert<span class="token punctuation">;</span>

<span class="token keyword">function</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token class-name">Array</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>arguments<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">prev<span class="token punctuation">,</span> curr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> prev <span class="token operator">+</span> curr<span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token function">describe</span><span class="token punctuation">(</span><span class="token string">'add()'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">var</span> tests <span class="token operator">=</span> <span class="token punctuation">[</span>
    <span class="token punctuation">{</span>args<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span>       expected<span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>args<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span>    expected<span class="token operator">:</span> <span class="token number">6</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>args<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">,</span> expected<span class="token operator">:</span> <span class="token number">10</span><span class="token punctuation">}</span>
  <span class="token punctuation">]</span><span class="token punctuation">;</span>

  tests<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">test</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">it</span><span class="token punctuation">(</span><span class="token string">'correctly adds '</span> <span class="token operator">+</span> test<span class="token punctuation">.</span>args<span class="token punctuation">.</span>length <span class="token operator">+</span> <span class="token string">' args'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">var</span> res <span class="token operator">=</span> <span class="token function">add</span><span class="token punctuation">.</span><span class="token function">apply</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">,</span> test<span class="token punctuation">.</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span>
      assert<span class="token punctuation">.</span><span class="token function">equal</span><span class="token punctuation">(</span>res<span class="token punctuation">,</span> test<span class="token punctuation">.</span>expected<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><h2 id="测试耗时"><a href="#测试耗时" class="header-anchor">#</a> 测试耗时</h2> <p>很多的测试报告都会显示测试所花费的时间，同样也会对一些耗时的测试作出特殊的标记。</p> <p>我们可以使用slow()方法来明确的表示出，超过多久的时间，这个测试就可以认为是slow的。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token function">describe</span><span class="token punctuation">(</span><span class="token string">'something slow'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">slow</span><span class="token punctuation">(</span><span class="token number">10000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token function">it</span><span class="token punctuation">(</span><span class="token string">'should take long enough for me to go make a sandwich'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// ...</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h2 id="测试超时"><a href="#测试超时" class="header-anchor">#</a> 测试超时</h2> <ul><li>测试集合超时</li> <li>测试用例超时</li> <li>钩子函数超时</li> <li><code>this.timeout(500);</code>设置超时时间</li> <li><code>this.timeout(0);</code>禁止超时时间的判断</li> <li>内层定义的超时时间会覆盖外层的</li></ul> <h2 id="安装使用"><a href="#安装使用" class="header-anchor">#</a> 安装使用</h2> <p>安装：npm install mocha</p> <p>使用：使用mocha指令</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>格式：mocha [debug] [options] [files]

命令：
    init &lt;path&gt; : 生成一个在浏览器中运行的单元测试的模版
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>接口：mocha的测绘接口类型指的是集中测试用例组织模式的选择。Mocha提供了BDD,TDD,Exports,QUnit和Require-style几种接口。</p> <p><a href="https://segmentfault.com/a/1190000011362879#articleHeader22" target="_blank" rel="noopener noreferrer">接口提供的具体方法<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p> <h2 id="测试报告"><a href="#测试报告" class="header-anchor">#</a> <a href="https://segmentfault.com/a/1190000011362879#articleHeader23" target="_blank" rel="noopener noreferrer">测试报告<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></h2></div> <footer class="page-edit" style="display:none;"><!----> <!----></footer> <!----></main> <!----> <div style="display:none;" data-v-319dd33c data-v-319dd33c><div class="comments-wrapper" data-v-319dd33c><div class="valine-wrapper"><div id="valine"></div></div></div></div></div></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-a81d141e data-v-a81d141e><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-a81d141e><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-a81d141e></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-a81d141e></path></svg></div></div></div>
    <script src="/assets/js/app.ff77de4f.js" defer></script><script src="/assets/js/4.6c1b7be2.js" defer></script><script src="/assets/js/1.3ea07518.js" defer></script><script src="/assets/js/16.b24685ca.js" defer></script>
  </body>
</html>
